package com.wn.main.ui.category.view

import android.os.Bundle
import androidx.core.view.isVisible
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.GsonUtils
import com.google.gson.reflect.TypeToken
import com.wn.common.constant.KEY_LIST
import com.wn.common.model.CategorySecondItem
import com.wn.framework.base.BaseFragment
import com.wn.framework.ext.dividerGridSpace
import com.wn.main.R
import com.wn.main.databinding.FragmentCategoryViewPagerBinding
import com.wn.main.ui.category.adapter.CategoryViewAdapter
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

/**
 * @Author: wn
 * @Date: 2025/4/11 15:14
 * @Description: 分类页 右侧 内容 Fragment
 */
@AndroidEntryPoint
class CategoryViewPagerFragment : BaseFragment<FragmentCategoryViewPagerBinding>() {

  override fun getLayoutRes() = R.layout.fragment_category_view_pager

  @Inject
  lateinit var mAdapter: CategoryViewAdapter

  override fun initView() {
    mBinding.recyclerView.apply {
      adapter = mAdapter
      layoutManager = GridLayoutManager(requireContext(), 2) // 网格流
      dividerGridSpace(2, 10.0f, true) // 设置分割线
    }
  }

  override fun initData() {
    // 获取传递 的  list
    val listJson = arguments?.getString(KEY_LIST, "")
    // string  转 Bean
    val type = object : TypeToken<MutableList<CategorySecondItem>>() {}.type
    val list = GsonUtils.fromJson<MutableList<CategorySecondItem>>(listJson, type)

    mBinding.viewEmptyData.isVisible = list.isNullOrEmpty()
    mAdapter.setData(list)
  }

  companion object {
    fun newInstance(jsonStr: String): CategoryViewPagerFragment =
      CategoryViewPagerFragment().apply {
        arguments = Bundle().apply { putString(KEY_LIST, jsonStr) }
      }
  }
}